<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005-2009 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  Leo Feyer 2005-2009
 * @author     Leo Feyer <leo@typolight.org>
 * @package    Backend
 * @license    LGPL
 * @filesource
 */


/**
 * Class ModuleMaintenance
 *
 * Back end module "maintenance".
 * @copyright  Leo Feyer 2005-2009
 * @author     Leo Feyer <leo@typolight.org>
 * @package    Controller
 */
class ModuleOrderExport extends BackendModule
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'be_orderexport';


	/**
	 * Generate module
	 */
	protected function compile()
	{
		$this->loadLanguageFile('tl_orderexport');
		
		$this->exportOrder();
		
		$this->Template->href = $this->getReferer(true);
		$this->Template->title = specialchars($GLOBALS['TL_LANG']['MSC']['backBT']);
		$this->Template->action = ampersand($this->Environment->request, true);
		$this->Template->selectAll = $GLOBALS['TL_LANG']['MSC']['selectAll'];
		$this->Template->button = $GLOBALS['TL_LANG']['MSC']['backBT'];
		$this->Template->sectionTitle = $GLOBALS['TL_LANG']['tl_orderexport']['exportTitle'];
		$this->Template->catalogSkuid = $GLOBALS['TL_LANG']['tl_orderexport']['catalogSkuid'];
		$this->Template->exortSubmit = $GLOBALS['TL_LANG']['tl_orderexport']['exortSubmit'];
	}
	
	/**
	 * Handle the html 
	 */
	protected function exportOrder()
	{
		// Truncate cache tables
		if ($this->Input->post('FORM_SUBMIT') == 'export')
		{
			if ($this->Input->post('skuid'))
			{
				$arrOrders = array();
				$this->import("Database");
				
				$objOrders = $this->Database->prepare("SELECT odr.* 
														FROM `tl_orders` AS odr
														LEFT JOIN `tl_order_detail` AS odd ON odr.`id`=odd.`pid` 
														LEFT JOIN `product_catalog` AS cata ON cata.`id`=odd.`product_catalog_id` 
														WHERE cata.`skuid`=?
														GROUP BY odr.`id`
														ORDER BY odr.`id")
											->execute($this->Input->post('skuid'));
				if ($objOrders->numRows < 1)
					return;
				
				$arrColumn[0] = array(
					'Catalog ID', 'Order Number', 'Carrier ID', 'Language ID', 'Member ID', 'Currency ID', 'Delivery Address ID', 'Billing Address ID', 'Order Generate Time', 'Payment Method', 'Total discount', 'Total Real Paid', 'Total Catalog', 'Total Shipping Fee', 'Total Wrapping Fee', 'Order Status', 'Date Created', 'Date Updated'
				);
				
				while ($objOrders->next())
				{
					$arrOrders[] = array(
						'id' => $objOrders->id,
						'orderno' => $objOrders->orderno,
						'id_carrier' => $objOrders->id_carrier,
						'id_lang' => $objOrders->id_lang,
						'id_member' => $objOrders->id_member,
						'id_currency' => $objOrders->id_currency,
						'id_address_delivery' => $objOrders->id_address_delivery,
						'id_address_invoice' => $objOrders->id_address_invoice,
						'tstamp' => date("Y-m-d H:i:s", $objOrders->tstamp),
						'payment' => $objOrders->payment,
						'total_discounts' => $objOrders->total_discounts,
						'total_paid_real' => $objOrders->total_paid_real,
						'total_catolog' => $objOrders->total_catolog,
						'total_shipping' => $objOrders->total_shipping,	
						'total_wrapping' => $objOrders->total_wrapping,
						'odr_status' => $objOrders->odr_status,
						'date_add' => $objOrders->date_add,
						'date_upd' => $objOrders->date_upd
					);						
				}
				
				$arrOrders = array_merge($arrColumn, $arrOrders);
				//print_r($arrOrders);exit;
				include_once(TL_ROOT."/plugins/phpexcel/excel-xml.php");
				$objExcel = Excel_XML_Generate::getInstance();
				//print_r($objExcel);exit;
				$objExcel->generateExcel('order_' . date("Y-m-d"), $arrOrders);
			}
		}
	}
}

?>